<template>
  <div class="block">
    <div id="app">
      <div class="search-container">
        <div class="search-box">
          <div class="label">职位名称</div>
          <el-input v-model="searchName" placeholder="请输入搜索内容"></el-input>
        </div>
        <div class="search-box">
          <div class="label">职位要求</div>
          <el-input v-model="searchRequire" placeholder="请输入搜索内容"></el-input>
        </div>
        <div class="option-selector">
          <div class="label">招聘年级</div>
          <el-select v-model="selectedOption" placeholder="请选择招聘年级" class="wide-select">
            <el-option v-for="option in options" :key="option.value" :label="option.label"
              :value="option.value"></el-option>
          </el-select>
        </div>
        <el-button type="danger" class="button primary" @click="loadData">搜索</el-button>
        <el-button type="danger" class="button1 primary" @click="dialogVisible = true">新增</el-button>
      </div>
    </div>

    <div>
      <el-dialog class="add" title="新增招聘" :visible.sync="dialogVisible">
        <el-form :model="form" :rules="rules" ref="form" label-width="80px">
          <el-form-item label="职位名称" prop="title">
            <el-input style="width: 400px;" v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="工作内容" prop="work">
            <el-input style="width: 400px;" type="textarea" v-model="form.work"></el-input>
          </el-form-item>
          <el-form-item label="职位要求" type="textarea" prop="requirement">
            <el-input style="width: 400px;" v-model="form.requirement"></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="label">
            <el-select v-model="form.label" placeholder="请选择年级" style="width: 400px;">
              <el-option v-for="option in options" :key="option.value" :label="option.label"
                :value="option.value" ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="描述"  prop="description">
            <el-input style="width: 400px;" type="textarea" v-model="form.description"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
      </el-dialog>

    </div>

    <el-table :data="comtableData" show-header>
      <el-table-column prop="title" width="150px" label="职位名称">
      </el-table-column>
      <el-table-column prop="work" type="textarea" width="300px" label="工作内容">
        <template slot-scope="scope">
          <!-- 使用 truncate 过滤器截断文本 -->
          {{ scope.row.work | truncate }}
        </template>
      </el-table-column>
      <el-table-column prop="requirement" type="textarea" width="300px" label="职位要求">
        <template slot-scope="scope">
          <!-- 使用 truncate 过滤器截断文本 -->
          {{ scope.row.requirement | truncate }}
        </template>
      </el-table-column>
      <el-table-column prop="label" width="90px" label="类型">
        <template slot-scope="scope">
          <span v-if="scope.row.label === 1">大一</span>
          <span v-if="scope.row.label === 2">大二</span>
          <span v-if="scope.row.label === 3">大三</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" width="150px" label="创建时间">
      </el-table-column>
      <el-table-column prop="description" type="textarea" width="400px" label="描述">
        <template slot-scope="scope">
          <!-- 使用 truncate 过滤器截断文本 -->
          {{ scope.row.description | truncate }}
        </template>
      </el-table-column>
      <el-table-column width="100px" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="danger" @click="handleEdit(scope.row)">修改</el-button>
          <div style="margin-top:10px">
            <el-button size="small" type="danger" @click="deletehandleEdit(scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog label="编辑招聘信息" :visible.sync="eddialogVisible">
      <el-form :model="editForm" :rules="rules" ref="editForm" label-width="70px">
        <el-form-item  label="职位名称" prop="title">
          <el-input style="width: 400px;" v-model="editForm.title"></el-input>
        </el-form-item>
        <el-form-item label="工作内容" prop="work">
          <el-input style="width: 400px;" type="textarea" v-model="editForm.work"></el-input>
        </el-form-item>
        <el-form-item label="职位要求" prop="requirement">
          <el-input style="width: 400px;" type="textarea" v-model="editForm.requirement"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="label">
          <el-select v-model="editForm.label" placeholder="请选择年级" style="width: 400px;">
            <el-option v-for="option in options" :key="option.value" :label="option.label"
              :value="option.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input style="width: 700px; " type="textarea" v-model="editForm.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="eddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>


      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'App',
  inject: ['reload'],
  data() {
    return {
      headers: {
        'token': 'Bearer initial'
      },
      uploadParams: {
        id: null
      },
      limitNum: 1,
      searchName: '',
      searchRequire:'',
      dateRange: ['', ''],
      beginTime: "",
      endTime: "",
      selectedOption: '',
      options: [
        { value: '1', label: '大一' },
        { value: '2', label: '大二' },
        { value: '3', label: '大三' },

      ],
      comtableData: [],
      date: '',
      page: 1,
      pageSize: 15,
      search: '',
      dialogVisible: false,
      eddialogVisible: false,
      deleteTask: false,
      form: {
        work: '',
        title: '',
        requirement: '',
        createTime: '',
        label: '',
        description: '',
      },
      editForm: {},
      rules: {
        title: [
          { required: true, message: '请输入职位名称', trigger: 'blur' }
        ],
        work: [
          { required: true, message: '请输入工作内容', trigger: 'blur' }
        ],
        requirement: [
          { required: true, message: '请输入职位要求', trigger: 'blur' }
        ],
        description: [
          { required: false, message: '请输入描述', trigger: 'blur' }
        ],
      }

    }

  },
  created() {
    this.loadData();
    this.headers['token'] = `Bearer ${this.$getToken()}`;
  },
  methods: {
    convertTimestamp(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      // const hours = String(date.getHours()).padStart(2, '0');
      // const minutes = String(date.getMinutes()).padStart(2, '0');
      // const seconds = String(date.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} `;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.page = 1
      this.loadData()

    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val
      this.loadData()
    },

    loadData() {
      // 找到selectedOption对应的选项对象
      const selectedOptionObj = this.options.find(option => option.value === this.selectedOption);
      const query = {
        pageNo: this.page,
        pageSize: this.pageSize,
        label: selectedOptionObj ? selectedOptionObj.value : null, // 使用三元运算符确保label不为undefined
        title: this.searchName,
        requirement: this.searchRequire
      };
      axios.get(`api/employ/page`, { params: query }).then((res) => {
        const convertedList = res.data.list.map(item => {
          return {
            ...item,
            date: this.convertTimestamp(item.date) // 转换日期
          };
        });
        this.comtableData = convertedList
        this.total = res.data.total
      }).catch((error) => {
        console.error(error);
        this.$message.error('网络错误')
      });
    },
    submitForm() {
      if (!this.form.work) {
        this.$message.error('工作内容不能为空');
        return;
      }
      // 检查 title 字段是否为空
      if (!this.form.requirement) {
        this.$message.error('职位要求不能为空');
        return;
      }
      // 检查 title 字段是否为空
      if (!this.form.title) {
        this.$message.error('职位名称不能为空');
        return;
      }
      if (!this.form.label) {
        this.$message.error('类型不能为空');
        return;
      }
      axios.post(`api/employ/add`, this.form).then((res) => {
        console.log(res);
        if (res.status == '200') {
          this.dialogVisible = false
          this.$message.success('新增成功')
          this.reload()
        } else {
          console.log('error submit!!')
          return false
        }
      })


    },


    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(() => {
          done();
        })
        .catch(() => { });
    },
    cancelDelete() {
      this.deleteTask = false;
    },
    handleEdit(row) {
      this.editForm = { ...row }; // 创建编辑表单的数据副本
      this.eddialogVisible = true; // 显示对话框
    },
    saveEdit() {
      axios.put(`api/employ/update`, this.editForm).then((res) => {
        if (res.status == '200') {
          this.eddialogVisible = false; // 关闭对话框
          this.$message.success('修改信息成功')
          this.reload()
        } else {
          this.$message.success('修改信息失败，请联系开发者')
          return false
        }
      });
    },

    deletehandleEdit(row) {
      if (row.id != null) {
        let id = row.id;
        if (id == null) {
          this.$message.error('请选择需要删除的员工。')
        } else {
          axios.delete(`api/employ/delete/${id}`).then(() => {
            this.$message.success('删除成功')
            this.reload()
          })
        }
      } else {
        this.$message.error('请先选择需要删除的员工。');
      }
    }

  }
}
</script>

<style>
.search-container {
  margin-bottom: 20px;
}

#app {
  /* font-family: Avenir, Helvetica, Arial, sans-serif; */
  font-family: PingFangSC-Medium;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  background-color: #fff;
  color: #606266;
  border-radius: 20px;
}

.search-container {

  display: flex;
  justify-content: center;
  align-items: center;


}

.search-container>div {
  margin-right: 70px;
  margin-bottom: 20px;
}

.label {
  margin-top: 30px;
  text-align: left;
  margin-bottom: 20px;
  font-weight: bold;

}

.button {
  font-family: PingFangSC-Medium;
  width: 90px;
  height: 50px;
  background: #f27657;
  border-radius: 20px;
}

.button1 {
  font-family: PingFangSC-Medium;
  width: 90px;
  height: 50px;
  background: #f3a895;
  border-radius: 20px;
}

.button:hover {
  background: #F16440;
  border: 0;
}
</style>